﻿Public Class DimList_ToolWindow

    Private _ParentForm As MainForm
    Private WithEvents _MetisCore As Core.MetisCore
    Private WithEvents _DimList As Core.Dimensions.MetisDimensionList

    Sub New()
        InitializeComponent()
    End Sub

    Sub Init(ByVal MetisCore As Core.MetisCore, ByVal ParentForm As MainForm)
        _MetisCore = MetisCore
        _ParentForm = ParentForm
        If _MetisCore.Connection.State = Data.ConnectionState.Open Then Reload()
    End Sub

    Private Sub HandleConStateChanged(ByVal sender As Object, ByVal e As System.Data.StateChangeEventArgs) Handles _MetisCore.ConnectionStateChange
        If e.CurrentState = Data.ConnectionState.Closed OrElse e.CurrentState = Data.ConnectionState.Broken Then
            tvDimList.Nodes.Clear()
            btnDim_Add.Enabled = False
            btnDim_Del.Enabled = False
            btnDim_Open.Enabled = False
            btnReloadDimList.Enabled = False
        ElseIf e.CurrentState = Data.ConnectionState.Open AndAlso e.OriginalState = Data.ConnectionState.Closed Then
            _DimList = _MetisCore.Dimensions
            btnDim_Add.Enabled = True
            btnReloadDimList.Enabled = True
            Reload()
        End If
    End Sub

    Private Sub HandleDimensionListChanged(ByVal sender As Object, ByVal e As System.ComponentModel.ListChangedEventArgs) Handles _DimList.ListChanged

        If e.ListChangedType = System.ComponentModel.ListChangedType.ItemChanged Then
            Dim tmpDim As Core.Dimensions.MetisDimension = _DimList(e.NewIndex)
            Dim node As TreeNode = FindNodeByDimension(tmpDim)
            If node IsNot Nothing AndAlso Not tmpDim.IsModified Then
                node.Text = tmpDim.Name
                node.ToolTipText = tmpDim.Annotation
            End If

        ElseIf e.ListChangedType = System.ComponentModel.ListChangedType.ItemAdded Then
            Dim tmpDim As Core.Dimensions.MetisDimension = _DimList(e.NewIndex)
            Dim newNode As TreeNode
            If tvDimList.Nodes.Count = 0 Then
                newNode = tvDimList.Nodes.Add(tmpDim.ID.ToString, tmpDim.Name, 0, 0)
            Else
                Dim index As Integer = 0
                For Each item As TreeNode In tvDimList.Nodes
                    If item.Text <= tmpDim.Name Then
                        index += 1
                    Else
                        Exit For
                    End If
                Next
                newNode = tvDimList.Nodes.Insert(index, tmpDim.ID.ToString, tmpDim.Name, 0, 0)
            End If
            newNode.Tag = tmpDim
            newNode.ToolTipText = tmpDim.Annotation

        ElseIf e.ListChangedType = System.ComponentModel.ListChangedType.ItemDeleted Then
            For Each node As TreeNode In tvDimList.Nodes
                If TypeOf node.Tag Is Core.Dimensions.MetisDimension AndAlso _
                   _MetisCore.Dimensions.Find(DirectCast(node.Tag, Core.Dimensions.MetisDimension).ID) Is Nothing Then
                    tvDimList.Nodes.Remove(node)
                End If
            Next


        End If
    End Sub

    Private Function FindNodeByDimension(ByVal dimension As Core.Dimensions.MetisDimension) As TreeNode
        For Each node As TreeNode In tvDimList.Nodes
            If dimension.Equals(node.Tag) Then Return node
        Next
        Return Nothing
    End Function

    Public Sub Reload()
        Try
            Me.Cursor = Cursors.WaitCursor
            tvDimList.SuspendLayout()
            tvDimList.Nodes.Clear()
            For Each item As Core.Dimensions.MetisDimension In _DimList
                Dim newNode As TreeNode = tvDimList.Nodes.Add(item.ID.ToString, item.Name, 0, 0)
                newNode.Tag = item
                'tnTmpNode.ContextMenuStrip = mnuFunctionTreeNode
                newNode.ToolTipText = item.Annotation
                'newNode.Nodes.Add("FIELDS", My.Resources.Metis_FuncList_Fields.ToString, 1,1)
                'newNode.Nodes.Add("USES", My.Resources.Metis_FuncList_Uses.ToString, 1,1)
                newNode.Nodes.Add("USED", My.Resources.Metis_FuncList_Used.ToString, 1, 1)
            Next
        Finally
            tvDimList.ResumeLayout()
            Me.Cursor = Cursors.Default
            btnDim_Del.Enabled = tvDimList.SelectedNode IsNot Nothing
            btnDim_Open.Enabled = tvDimList.SelectedNode IsNot Nothing
        End Try
    End Sub

    Public Property MetisCoreReference() As Core.MetisCore
        Get
            Return _MetisCore
        End Get
        Set(ByVal value As Core.MetisCore)
            _MetisCore = value
            Reload()
        End Set
    End Property

    Private Sub HandleReloadDimListClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReloadDimList.Click, miRefresh.Click
        _MetisCore.ReloadDimensions()
        Reload()
    End Sub

    Private Sub mnuDim_Opening(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles mnuDim.Opening
        miDimension_Del.Enabled = btnDim_Del.Enabled
        miDimension_Open.Enabled = btnDim_Open.Enabled
    End Sub

    Private Sub tvDimList_AfterSelect(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles tvDimList.AfterSelect
        btnDim_Del.Enabled = (tvDimList.SelectedNode IsNot Nothing AndAlso TypeOf tvDimList.SelectedNode.Tag Is Core.Dimensions.MetisDimension)
        btnDim_Open.Enabled = (tvDimList.SelectedNode IsNot Nothing AndAlso TypeOf tvDimList.SelectedNode.Tag Is Core.Dimensions.MetisDimension)
    End Sub

    Private Sub tvDimList_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles tvDimList.MouseDown
        Dim tmpItem As TreeNode = tvDimList.GetNodeAt(e.Location)
        If tmpItem IsNot Nothing Then tvDimList.SelectedNode = tmpItem
    End Sub

    Private Sub HandleDimOpenClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDim_Open.Click, miDimension_Open.Click, tvDimList.NodeMouseDoubleClick
        If tvDimList.SelectedNode IsNot Nothing AndAlso _
           (TypeOf tvDimList.SelectedNode.Tag Is Core.Dimensions.MetisDimension OrElse _
            TypeOf tvDimList.SelectedNode.Tag Is Core.Functions.MetisFunction) Then
            _ParentForm.OpenMetisEntity(tvDimList.SelectedNode.Tag)
        End If

    End Sub

    Private Sub HandleDimAddClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDim_Add.Click, miDimension_Add.Click
        _ParentForm.OpenMetisEntity(New Core.Dimensions.MetisDimension(_MetisCore))
    End Sub

    Private Sub HandleDimDelClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDim_Del.Click, miDimension_Del.Click
        If MsgBox(String.Format(My.Resources.Metis_Dim_ConfirmDrop.ToString, DirectCast(tvDimList.SelectedNode.Tag, Core.Dimensions.MetisDimension).Name), _
                  MsgBoxStyle.YesNo Or MsgBoxStyle.Question) = MsgBoxResult.Yes Then
            DirectCast(tvDimList.SelectedNode.Tag, Core.Dimensions.MetisDimension).Drop()
        End If
    End Sub

    Private Sub tvDimList_AfterExpand(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles tvDimList.AfterExpand
        If TypeOf e.Node.Tag Is Core.Dimensions.MetisDimension Then
            e.Node.Nodes(0).Nodes.Clear()
            For Each item As Core.Functions.MetisFunction In DirectCast(e.Node.Tag, Core.Dimensions.MetisDimension).UsedBy
                Dim newNode As TreeNode = e.Node.Nodes(0).Nodes.Add(item.ID.ToString, item.FullName, 1, 1)
                newNode.Tag = item
                newNode.ToolTipText = item.Annotation
                newNode.Nodes.Add("FIELDS", My.Resources.Metis_FuncList_Fields.ToString, 1, 1)
                newNode.Nodes.Add("USES", My.Resources.Metis_FuncList_Uses.ToString, 1, 1)
                newNode.Nodes.Add("USED", My.Resources.Metis_FuncList_Used.ToString, 1, 1)
            Next
        ElseIf TypeOf e.Node.Tag Is Core.Functions.MetisFunction Then
            Dim tmpFunc As Core.Functions.MetisFunction = e.Node.Tag
            e.Node.Nodes("FIELDS").Nodes.Clear()
            For Each field As Core.Functions.MetisFunctionField In tmpFunc.Fields
                Dim newNode As TreeNode = e.Node.Nodes("FIELDS").Nodes.Add(field.ID.ToString, field.Name, 2, 2)
                newNode.Tag = field
                newNode.ToolTipText = field.Annotation
                If field.PK Then newNode.NodeFont = New Font(tvDimList.Font, FontStyle.Bold)
            Next
            e.Node.Nodes("USES").Nodes.Clear()
            For Each func As Core.Functions.MetisFunction In tmpFunc.Uses
                Dim newNode As TreeNode = e.Node.Nodes("USES").Nodes.Add(func.ID.ToString, func.FullName, 1, 1)
                newNode.Tag = func
                newNode.ToolTipText = func.Annotation
                newNode.Nodes.Add("FIELDS", My.Resources.Metis_FuncList_Fields.ToString, 1, 1)
                newNode.Nodes.Add("USES", My.Resources.Metis_FuncList_Uses.ToString, 1, 1)
                newNode.Nodes.Add("USED", My.Resources.Metis_FuncList_Used.ToString, 1, 1)
            Next
            e.Node.Nodes("USED").Nodes.Clear()
            For Each func As Core.Functions.MetisFunction In tmpFunc.UsedBy
                Dim newNode As TreeNode = e.Node.Nodes("USED").Nodes.Add(func.ID.ToString, func.FullName, 1, 1)
                newNode.Tag = func
                newNode.ToolTipText = func.Annotation
                newNode.Nodes.Add("FIELDS", My.Resources.Metis_FuncList_Fields.ToString, 1, 1)
                newNode.Nodes.Add("USES", My.Resources.Metis_FuncList_Uses.ToString, 1, 1)
                newNode.Nodes.Add("USED", My.Resources.Metis_FuncList_Used.ToString, 1, 1)
            Next
        End If
    End Sub
End Class
